package com.cargo.base.utils;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtil {
	
	private static final String ak = "XBJ0RaBKRGD6aYLx";
	private static final String sk = "xb5rKKB7p23CGTL0VSV9XyVsOayBd1";
	private static final String bucketName = "eshipping-filestore";
	private static final String endPoint = "http://oss-cn-hangzhou.aliyuncs.com";

	/**
	 * saveFile:(文件上传).
	 * @param fileName
	 * @param file
	 * @return 
	 * @return :String 
	 * @Creator:wizard
	 * @Date:2016年5月18日 下午2:00:47
	 */
	public  String saveFile(String fileName,MultipartFile file) {
		String fileURL ="";
		String aliFilePath = PropertiesUtil.getValue("common.fileurl");
		fileName = aliFilePath.substring(aliFilePath.indexOf("aliyuncs.com") + 13) + fileName;
		OSSClient client = new OSSClient(endPoint, ak, sk);
		try {
			client.putObject(bucketName, fileName, file.getInputStream());
			fileURL = bucketName + "." + endPoint.split("//")[1] + "/" + fileName;
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			client.shutdown();
		}
		return fileURL;
	}
	
	/**
	 * 
	 * getFileInputStream:(根据文件路径从阿里云上获得文件流).
	 * @param filePath
	 * @return 
	 * @return :InputStream 
	 * @Creator:wizard
	 * @Date:2016年9月18日 下午4:27:31
	 */
	public static void downloadFile(String alifileName,HttpServletResponse response)throws Exception{
		OSSClient client = new OSSClient(endPoint, ak, sk);
		InputStream is = null;
		OutputStream os = null;
		try {
			
			String aliFilePath = PropertiesUtil.getValue("common.fileurl");
			alifileName = aliFilePath.substring(aliFilePath.indexOf("aliyuncs.com") + 13) + alifileName;
			
			OSSObject ossObj = client.getObject(bucketName, alifileName);
			
			is = ossObj.getObjectContent();
			
			os = response.getOutputStream();
			
			byte[] b = new byte[2048];
			int length;
			while ((length = is.read(b)) > 0) {
				os.write(b, 0, length);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(CheckUtil.checkObj(os)){
				os.close();
			}
			if(CheckUtil.checkObj(is)){
				is.close();
			}
			if(CheckUtil.checkObj(client)){
				client.shutdown();
			}
		}
	}

	/** 
	 * saveFormatFile:(自定义上传文件方法).
	 * @param fileName
	 * @param file
	 * @param folder 
	 * @return 
	 * @return :String 
	 * @Creator:wizard
	 * @Date:2017年3月10日 下午5:43:39
	 */  
	public String saveFormatFile(String fileName, MultipartFile file, String folder) {
		String fileURL ="";
		String aliFilePath = PropertiesUtil.getValue("common.fileurl");
		fileName = aliFilePath.substring(aliFilePath.indexOf("aliyuncs.com") + 13) + fileName;
		OSSClient client = new OSSClient(endPoint, ak, sk);
		try {
			client.putObject(bucketName, fileName, file.getInputStream());
			fileURL = bucketName + "." + endPoint.split("//")[1] + "/" + fileName;
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			client.shutdown();
		}
		return fileURL;
	}
}
